import CryptoJS from 'crypto-js'

const KEY = 'sdskwlkj'

// DES加密
export const encryptDes = (message) => {
  const keyHex = CryptoJS.enc.Utf8.parse(KEY)
  const option = {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }
  const encrypted = CryptoJS.DES.encrypt(
    message,
    keyHex,
    option
  )
  return encrypted.ciphertext.toString(CryptoJS.enc.Base64)
}

// DES解密
export const decryptDes = (message) => {
  const keyHex = CryptoJS.enc.Utf8.parse(KEY)
  const decrypted = CryptoJS.DES.decrypt(
    {
      ciphertext: CryptoJS.enc.Hex.parse(message)
    },
    keyHex,
    {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    }
  )
  return decrypted.toString(CryptoJS.enc.Utf8)
}
